package javatest;

import com.alibaba.fastjson.JSONObject;
import framework.sepal4java.common.socket.CmdMessage;
import framework.sepal4java.common.struct.ConstantEnum.ResourceType;
import framework.sepal4java.common.struct.ResourceConfig;
import framework.sepal4java.lib4wrapper.WrapperRemoteConnector;

import java.util.List;

public class SensorDemo1 {
    public static void main(String[] args) {
        ResourceConfig config = new ResourceConfig("Sensor1", ResourceType.SENSOR, List.of("field1", "field2"));
        WrapperRemoteConnector connector = WrapperRemoteConnector.getInstance();
        if (connector.register("127.0.0.1", 9091, config)) {
            while (true) {
                CmdMessage msg = connector.recv();
                switch (msg.cmd) {
                    case "sensory_request":
                        JSONObject value = new JSONObject();
                        value.put("field1", "mock_data1");
                        value.put("field2", "mock_data2");
                        CmdMessage response = new CmdMessage("sensory_back", value.toJSONString());
                        connector.send(response);
                        break;
                    case "sensory_close":
                        connector.close();
                        return;
                }
            }
        }
    }
}